package com.example.demo.serviceimpl;

import com.example.demo.entity.Category;
import com.example.demo.repository.CategoryRepository;
import com.example.demo.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImp implements CategoryService {
    @Autowired
    CategoryRepository categoryRepository;


    @Override
    public Category getCategoryById(Long id) {
        return categoryRepository.getOne(id);
    }

    @Override
    public List<Category> findAll() {
        return categoryRepository.findAll();
    }

    @Override
    public List<Category> findByName(String name) {
        return categoryRepository.findByNameLikeOrderByIdDesc("%"+name+"%");
    }

    @Override
    public List<Category> findById(Long id) {
        return categoryRepository.findCategoriesByById(id);
    }

    @Override
    public Page<Category> getCategoryList(Pageable pageable) {
        return categoryRepository.findAll(pageable);
    }

    @Override
    public boolean modify(Category category) {
        int count=categoryRepository.updateCategory(category.getName(),category.getId());

        return count > 0 ? true : false;
    }

    @Override
    public boolean delete(String name) {
        int count=categoryRepository.deleteByName(name);
        return count > 0 ? true : false;
    }

    @Override
    public boolean addCategory(Long id, String name) {
        int count=categoryRepository.addCatrgory(id,name);
        return count > 0 ? true : false;
    }

    @Override
    public boolean delete2(Long id) {
        int count=categoryRepository.deleteByCatId(id);
        return count > 0 ? true : false;
    }


}
